package edu.gatech.cs2340.sharks.fms;

/**
 * LostItemsActivity.java
 * @author The Invisible Man
 * 
 * Displays a list of the items that the logged-in user has lost
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import edu.gatech.cs2340.sharks.fms.data.*;
import java.util.List;

public class LostItemsActivity extends Activity
{
	private ListView ListView1;
	Button info;
	private static Activity instance = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState); 
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_lost_items);
		if (instance != null) instance.finish();
		instance = this;
		
		Packet<List<Item>> ui = DBMgr.userItems(DBMgr.user); 

		if (ui.status.code() == Status.Code.OK)
		{		
			List<Item> it = ui.value;
			Item[] items = new Item[it.size()];
			int num = 0; 
			
			for (int i = 0; i < it.size(); i++)
			{ 
				items[i] = it.get(i); // FIXME!
				if (it.get(i).status.equals(Item.Status.LOST)) num++; 
			}
			
			Item[] lost = new Item[num];
			int count = 0;
			for (int a = 0; a < items.length; a++)
			{
				if (count < num && items[a].status.equals(Item.Status.LOST))
				{
					lost[count] = items[a]; 
					count++;
				}
			}

			ListView1 = (ListView) findViewById(R.id.ListView1);
			ItemAdapter adaptItems = new ItemAdapter(this, R.layout.listview_item_row, lost);
			View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
			ListView1.addHeaderView(header);
			ListView1.setClickable(true); 
			TextView whichList = (TextView) findViewById(R.id.whichItems); 
			whichList.setText("Lost Items"); 
			ListView1.setSelectionAfterHeaderView();
			ListView1.setAdapter(adaptItems); 
		}
		else Info.alert(this, ui.status.toString());
	}
	
	/**
	 * Calls the Add Item Activity 
	 * 
	 * @param view
	 */
	public void addItem(View view)
	{
		Intent toAddItem = new Intent(this, AddItemActivity.class);
		startActivity(toAddItem);
	}
	
	public void back(View view)
	{
		Intent back = new Intent(this, HomeActivity.class); 
		startActivity(back); 
	}
	
	public void itemDetails(View view)
	{
		int itemID = (Integer) view.getTag(); 
		Intent toItemDetails = new Intent(this, ViewItemActivity.class);
		toItemDetails.putExtra("ItemID", itemID); 
		startActivity(toItemDetails); 
	}
}